package com.mayuhao.coursemanage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mayuhao.coursemanage.entity.CssCourse;
import com.mayuhao.coursemanage.entity.CssCourseVO;
import com.mayuhao.coursemanage.service.ICssCourseService;
import com.mayuhao.coursemanage.util.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 马宇豪
 * @since 2022-10-25
 */
@RestController
//@RequestMapping("/css-course")
public class CssCourseController {

    @Resource
    private ICssCourseService courseService;

    /**
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.mayuhao.coursemanage.entity.CssCourseVO>
     * @description 请修改方法描述
     * @methodName findPage
     * @params [pageNum, pageSize, searchId, searchName]
     * @paramType [java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/10/25 0025 下午 9:12
     * @email 1757442521@qq.com
     **/
    @GetMapping("/findpage")
    public Page<CssCourseVO> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "7") Integer pageSize,
            @RequestParam(defaultValue = "") String searchId,
            @RequestParam(defaultValue = "") String searchName) {
        return courseService.findPage(pageNum, pageSize, searchId, searchName);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName updateDepartment
     * @params [course]
     * @paramType [com.mayuhao.coursemanage.entity.CssCourse]
     * @author 马宇豪
     * @date 2022/10/26 0026 下午 2:38
     * @email 1757442521@qq.com
     **/
    @PutMapping("/update")
    public boolean updateCourse(@RequestBody CssCourse course) {
        if ("NaN".equals(course.getGoCourseTime())) {
            course.setGoCourseTime("");
        }
        return courseService.updateById(course);
    }


    /**
     * @return java.lang.String
     * @description 请修改方法描述
     * @methodName getEcharts
     * @params []
     * @paramType []
     * @author 马宇豪
     * @date 2022/11/12 0012 下午 9:35
     * @email 1757442521@qq.com
     **/
    @GetMapping("/echarts")
    public Result getEcharts() {
        ArrayList<Integer> xlist = new ArrayList<>();
        ArrayList<String> ylist = new ArrayList<>();
        for (CssCourseVO echart : courseService.getEcharts()) {
            xlist.add(echart.getCourseChooseNumber());
            ylist.add(echart.getCourseName());
        }
        Collections.reverse(xlist);
        Collections.reverse(ylist);

        HashMap<String, Object> map = new HashMap<>();
        map.put("x", xlist);
        map.put("y", ylist);
        return new Result(true, "dfdf", map);

    }

    @DeleteMapping("/delbatch/{ids}")
    public boolean deleteCourse(@PathVariable List ids) {
        return courseService.removeBatchByIds(ids);
    }

    @DeleteMapping("/delsingle/{id}")
    public boolean deleteCourse(@PathVariable Integer id) {
        return courseService.removeById(id);
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        courseService.export(response);
    }

    @GetMapping("/findcourse")
    public Result getCourse() {
        return new Result(true, "ok", courseService.list());
    }

    @PostMapping("/import")
    public void imp(MultipartFile file) throws Exception {
        courseService.imp(file);
    }


}
